package ui.activity

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.example.mynewsapp.R
import ui.fragment.LoginFragment
import ui.fragment.SignUpFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator


// 这个activity托管Login和SignUp Fragment
class AccountActivity : AppCompatActivity() {

    private lateinit var tabLayout: TabLayout
    private lateinit var viewPager2: ViewPager2
    private lateinit var adapter: AccountPageAdapter

    private val prefsName = "AppIntroPrefs"
    private val prefsUserLogin = "isUserLoggedIn"

    // 检查这个label,这个label在LoginFragment
    private val PREFS_NAME = "LoginPrefs"
    private val PREFS_IS_LOGGED_IN = "isLoggedIn"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_account)
        tabLayout = findViewById(R.id.tabLayout)
        viewPager2 = findViewById(R.id.viewPager2)

        initTabLayoutAndViewPager()
        // 检查用户是否已经登录
//        val prefs = getSharedPreferences(prefsName, MODE_PRIVATE)
//        val isUserLoggedIn = prefs.getBoolean(prefsUserLogin, false)

        // 检查登录状态
        val sharedPreferences =
            getSharedPreferences(PREFS_NAME, android.content.Context.MODE_PRIVATE)
        val isLoggedIn = sharedPreferences.getBoolean(PREFS_IS_LOGGED_IN, false)

        if (!isLoggedIn) {
            // 如果用户未登录，跳转到登录页面, 本activity就是登录页面
        } else {
            startActivity(Intent(this, MainActivity::class.java))
        }
    }

    private fun initTabLayoutAndViewPager() {
        tabLayout.addTab(tabLayout.newTab().setText("登录"))
        tabLayout.addTab(tabLayout.newTab().setText("注册"))

        val fragmentManager: FragmentManager = supportFragmentManager
        adapter = AccountPageAdapter(fragmentManager, lifecycle)
        viewPager2.adapter = adapter

        TabLayoutMediator(tabLayout, viewPager2) { tab, position ->
            tab.text = when (position) {
                0 -> "登录"
                1 -> "注册"
                else -> "登录"
            }
        }.attach()
    }


    // 加载fragment
    private fun loadFragment(fragmentReplace: Fragment) {
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.main_fragment, fragmentReplace)
            .commit()
    }

}


class AccountPageAdapter(
    fragmentManager: FragmentManager, lifecycle: Lifecycle
) : FragmentStateAdapter(fragmentManager, lifecycle) {
    override fun createFragment(position: Int): Fragment {
        return when (position) {
            0 -> LoginFragment()
            1 -> SignUpFragment()
            else -> LoginFragment()
        }
    }

    override fun getItemCount(): Int {
        // 2 个tab
        return 2
    }
}